#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

using System;

using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;
using System.Collections;
using System.Reflection;

namespace Thn.Interface.Vcl
{
    public partial class ErrorDialog : Form
    {
        #region Constructor
        public ErrorDialog(Exception ex)
        {
            InitializeComponent();
            mException = ex;
        }

        public ErrorDialog()
        {
            InitializeComponent();
        }

        #endregion

        #region Fields
        Exception mException = null;
        #endregion

        #region CreateParams
        //turn on drop-shadow mode
        //protected override CreateParams CreateParams
        //{
        //    [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
        //    get
        //    {
        //        CreateParams parameters = base.CreateParams;

        //        int CSmDROPSHADOW = 0x00020000;

        //        OperatingSystem system = Environment.OSVersion;
        //        bool DropShadowSupported = system.Platform == PlatformID.Win32NT;

        //        DropShadowSupported = DropShadowSupported && system.Version.CompareTo(new Version(5, 1, 0, 0)) >= 0;

        //        if (DropShadowSupported)
        //        {
        //            parameters.ClassStyle = (parameters.ClassStyle | CSmDROPSHADOW);
        //        }

        //        return parameters;
        //    }
        //}
        #endregion

        #region ShowGeneral
        bool mShowGeneral = true;
        void ShowGeneral()
        {
            if (mShowGeneral)
            {
                try
                {
                    txtMessage.Text = mException.Message;
                    txtSource.Text = mException.Source;
                    txtTargetMethod.Text = GetTargetMethodFormat(mException);
                    txtHelp.Text = mException.HelpLink;
                    txtStack.Text = mException.StackTrace;
                }
                catch { }
                mShowGeneral = false;
            }
        }

        private string GetTargetMethodFormat(Exception Ex)
        {
            string result = "";
            try
            {
                object[] objArray1 = new object[7] { "[", Ex.TargetSite.DeclaringType.Assembly.GetName().Name, "]", Ex.TargetSite.DeclaringType, "::", Ex.TargetSite.Name, "()" };
                result = string.Concat(objArray1);
            }
            catch { }
            return result;
        }
        #endregion

        #region ShowInnerException
        bool mShowInner = true;
        void ShowInner()
        {
            if (mShowInner)
            {
                Exception exception1 = mException;
                TreeNode node1 = null;
                TreeNode node2 = null;
                TreeNode node3 = null;
                TreeNode node4 = null;
                try
                {
                    this.tvInnerException.BeginUpdate();
                    while (exception1 != null)
                    {
                        node2 = new TreeNode(exception1.GetType().FullName);
                        node3 = new TreeNode(exception1.Message);
                        node4 = new TreeNode(this.GetTargetMethodFormat(exception1));
                        node2.Nodes.Add(node3);
                        node2.Nodes.Add(node4);
                        if (node1 != null)
                        {
                            node1.Nodes.Add(node2);
                        }
                        else
                        {
                            this.tvInnerException.Nodes.Add(node2);
                        }
                        node1 = node2;
                        exception1 = exception1.InnerException;
                    }
                    this.tvInnerException.EndUpdate();
                }
                catch { }

                mShowInner = false;
            }
        }
        #endregion

        #region ShowOther
        bool mShowOther = true;
        void ShowOther()
        {
            if (mShowOther)
            {
                try
                {
                    Hashtable hashtable1 = this.GetCustomExceptionInfo(mException);
                    IDictionaryEnumerator enumerator1 = hashtable1.GetEnumerator();
                    this.lstOtherInfo.Items.Clear();
                    this.lstOtherInfo.BeginUpdate();
                    while (enumerator1.MoveNext())
                    {
                        ListViewItem item1 = new ListViewItem(enumerator1.Key.ToString());
                        if (enumerator1.Value != null)
                        {
                            item1.SubItems.Add(enumerator1.Value.ToString());
                        }
                        lstOtherInfo.Items.Add(item1);
                    }
                    this.lstOtherInfo.EndUpdate();
                }
                catch { }
                mShowOther = false;
            }
        }

        private Hashtable GetCustomExceptionInfo(Exception Ex)
        {
            Hashtable hashtable1 = new Hashtable();
            try
            {
                PropertyInfo[] infoArray1 = Ex.GetType().GetProperties();
                for (int num1 = 0; num1 < infoArray1.Length; num1++)
                {
                    PropertyInfo info1 = infoArray1[num1];
                    Type type1 = typeof(Exception);
                    if (type1.GetProperty(info1.Name) == null)
                    {
                        hashtable1.Add(info1.Name, info1.GetValue(Ex, null));
                    }
                }
            }
            catch { }
            return hashtable1;
        }

        #endregion

        private void btnOkmClick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ErrorDialogmLoad(object sender, EventArgs e)
        {
            if (mException != null)
            {
                lblException.Text = mException.GetType().FullName;
                ShowGeneral();
            }
            else
            {
                tabInfo.Enabled = false;
                lblException.Text = "[Unknown exception]";
            }

        }

        private void tabInfomSelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabInfo.SelectedTab == tabInner) ShowInner();
            else if (tabInfo.SelectedTab == tabOther) ShowOther();
        }
    }
}